<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\result\Result;
use app\model\Category as ModelCategory;
use think\facade\Request;

class Category extends BaseController
{

    protected $middleware = [
        'auth',
        'checkPermission'
    ];

    public function index()
    {
        $pageNum = Request::param('pageNum');
        $pageSize = Request::param('pageSize');
        $roleList = ModelCategory::page($pageNum, $pageSize)->select();
        return Result::Success($roleList);
    }

    public function add()
    {
        $name = Request::post('name');
        $image = Request::post('image');
        $sort = Request::post('sort');

        $hasRole = ModelCategory::where('name', $name)->findOrEmpty();
        if ($hasRole->isEmpty()) {

            $category = new ModelCategory();
            $category->name = $name;
            $category->image = $image;
            $category->sort = $sort;
            $category->save();

            return Result::Success($category);
        } else {
            return Result::Error(500, '同名分类已存在');
        }
    }
}
